import {
  getPage
} from '../../utils/config';
import api from '../../utils/api';
import {
  http,
  $empty,
  $toast,
  storage,
  Validate
} from '../../utils/utils';
import wxApi from '../../utils/wxApi';
import uploadImage from '../../utils/aliyunoss/uploadAliyun';
const app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    jianjie: "企业介绍",
    junjia: "装修均价",
    mainActiveIndex: 0,
    activeId: [],
    max: 20,
    addressForm: {
      shopname: '',
      ShopDescript: '',
      servicezone: '',
      junjia: '',
      companydesc: '',
      service: '',
      fengge: '',
      Category:''
    },
  },
  /**
   * 店铺名称
   */
  shopnameInput(e) {
    this.setData({
      shopnameError: '',
      'addressForm.shopname': e.detail
    })
  },
  onClickNav({
    detail = {}
  }) {
    this.setData({
      mainActiveIndex: detail.index || 0,
    });
  },

  onClickItem({
    detail = {}
  }) {
    const {
      activeId
    } = this.data;

    const index = activeId.indexOf(detail.id);
    if (index > -1) {
      activeId.splice(index, 1);
    } else {
      activeId.push(detail.id);
    }

    this.setData({
      activeId
    });
  },
  serviceInput(e) {
    this.setData({
      serviceError: '',
      'addressForm.service': e.detail
    })
  },
  fenggeInput(e) {
    this.setData({
      fenggeError: '',
      'addressForm.fengge': e.detail
    })
  },
  /**
   * 简介
   */
  ShopDescriptInput(e) {
    this.setData({
      ShopDescriptError: '',
      'addressForm.ShopDescript': e.detail
    })
  },

  servicezoneInput(e) {
    this.setData({
      servicezoneError: '',
      'addressForm.servicezone': e.detail
    })
  },
  junjiaInput(e) {
    this.setData({
      junjiaError: '',
      'addressForm.junjia': e.detail
    })
  },
  companydescInput(e) {
    this.setData({
      companydescError: '',
      'addressForm.companydesc': e.detail
    })
  },
  /**
   * 提交保存
   */
  addressForm(e) {
    Validate.check(this, this.addressValidate, e.detail.value, (res) => {
      if (res) {
        const data = this.data;
        const userInfo = data.userInfo;

        const {
          shopname,
          ShopDescript,
          servicezone,
          junjia,
          companydesc,
          service,
          fengge,
          Category
        } = data.addressForm;
        const params = {
          action: 'updateshopinfo',
          shopname: shopname,
          ShopDescript: ShopDescript,
          servicezone: servicezone,
          junjia: junjia,
          companydesc: companydesc,
          service: service,
          fengge: fengge,
          Category:Category,
          ShopId: data.ShopId,
        }
        http.post(api.default, {
          data: params
        }).then((res) => {
          if (res.data.code == 0) {
            userInfo.data.shop = res.data.data;
            storage.set('userInfo', userInfo);
            $toast('修改成功', {
              icon: 'success'
            });
            const pages = getCurrentPages()[getCurrentPages().length - 2];

            wx.navigateBack({
              delta: 1,
              success() {
                if (pages !== undefined || pages !== null) {
                  pages.data.pageChange = 1;
                  pages.onLoad();
                }
              }
            });
          } else {
            $toast(res.data.errmsg, {
              icon: 'fail'
            });

          }
        });
      }
    });
  },
  onChange(event) {
    const { picker, value, index } = event.detail;
    this.setData({
      'addressForm.Category': value
    })
    this.toggle('categoryShow');
  },
/**
   * 城市选择open || close
   */
  pickerCategoryTap() {
    this.toggle('categoryShow');
  },
  toggle(type) {
    this.setData({
      [type]: !this.data[type]
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    //判断入口是否带参数
    app.getLogin().then((res) => {
      console.log(res.data);
      var ShopType = res.data.shop.ShopType; //2=建材门店；3=装修公司；4=设计师
      if (ShopType == 2) { 

      } else if (ShopType == 3) {

      } else if (ShopType == 4) {
        this.setData({
          jianjie: "详细介绍",
          junjia: "服务报价"
        });
      }
      this.setData({
        userInfo: res,
        logged: res.code,
        ShopId: res.data.shop.ShopId,
        ShopType: res.data.shop.ShopType,
        addressForm: {
          shopname: res.data.shop.ShopName,
          ShopDescript: res.data.shop.ShopDescript,
          servicezone: res.data.shop.servicezone,
          junjia: res.data.shop.junjia,
          companydesc: res.data.shop.companydesc,
          service: res.data.shop.service,
          fengge: res.data.shop.goodinstyle,
          Category:res.data.shop.Category
        }
      });
    })
    ////第一部分树形分类数据加载
    // http.post(api.default, {
    //   data: {
    //     action: 'gettreecategory'
    //   }
    // }, false).then((res) => {
    //   const {
    //     data
    //   } = res.data;
    //   console.log(res.data);
    //   this.setData({
    //     items: data
    //   });
    // });
    http.post(api.default, {
      data: {
        action: 'getselectcategory'
      }
    }, false).then((res) => {
      const {
        data
      } = res.data;
      console.log(res.data);
      this.setData({
        columns: data
      });
    });
    /**
     * 信息保存
     */
    Validate.rules('#shopname,#ShopDescript,#companydesc', (res) => {
      this.addressValidate = res;
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  }
})